import configService from './config.service.js'
let apiUrl = configService.apiUrl;
const request = (url, data, method = 'GET') => {
	return new Promise((resolve, reject) => {
		let header = {
			"Content-Type": "application/json"
		}
		const token = uni.getStorageSync('token');
		if (token) {
			header['token']  = token
		}
		uni.showLoading({
			title: ''
		});
		uni.request({
			url: apiUrl+url,
			data: data,
			method: method,
			header: header,
			success: res => {
				uni.hideLoading();
				
				if (res.statusCode == 200) {
					if (res.data.code === 200 || res.data.code === "0") {
						// console.log("request1",res)
						resolve(res.data)
					} else if (res.data.code === 401 || res.data.code === 4001) {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
						setTimeout(()=>{
							uni.removeStorageSync('lifeData') //移除token 和用户信息
							uni.navigateTo({
								url: '/pages/login/login'
							})
						},2000)
						
					}else {
						uni.showToast({
							title: res.data.msg,
							icon: "none"
						})
						resolve(res.data)
					}
				} else {
					reject(res)
				}
			},
			fail: res => {
				uni.hideLoading();
				reject(res)
			},
		})
	});
}
//不带token接口请求，首页
const request1 = (url, data, method = 'GET') => {
	return new Promise((resolve, reject) => {
		let header = {
			'Content-Type': 'application/json',
			'tenant': 'MDAwMA==',
		}
		showLoading()
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			success: res => {
				uni.hideLoading();
				if (res.data.code === "200" || data.code === "") {
					resolve(res.data)
				} else {
					reject(res)
				}
			},
			fail: res => {
				uni.hideLoading();
				reject(res)
			}
		})
	});
}

export { request, request1 };
